package org.idea.eaglemq.broker.core;

import org.idea.eaglemq.broker.cache.CommonCache;
import org.idea.eaglemq.broker.model.CommitLogMessageModel;
import org.idea.eaglemq.common.constants.BrokerConstants;
import org.idea.eaglemq.common.dto.MessageDTO;

import java.io.IOException;


public class CommitLogAppendHandler {

    public void prepareMMapLoading(String topicName) throws IOException {
        CommitLogMMapFileModel mapFileModel = new CommitLogMMapFileModel();
        mapFileModel.loadFileInMMap(topicName,0, BrokerConstants.COMMIT_LOG_DEFAULT_MMAP_SIZE);
        CommonCache.getCommitLogMMapFileModelManager().put(topicName,mapFileModel);
    }

    public void appendMsg(MessageDTO messageDTO) throws IOException {
        CommitLogMMapFileModel mapFileModel = CommonCache.getCommitLogMMapFileModelManager().get(messageDTO.getTopic());
        if(mapFileModel==null) {
            throw new RuntimeException("topic is invalid!");
        }
        mapFileModel.writeContent(messageDTO,true);
    }

}

